package ui.component
{
	import tool.tweener.Tweener;
	
	import events.GameEvent;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class ScrollBar extends Sprite
	{
		public var scrollWidth:int;
		public var scrollHeight:int;
		
		public var currentID:int=0;
		
		private var _itemWid:uint;
		
		private var _markMouseX:int;
		private var _markItemX:int;
		private var _vX:Number;
		private var _v0X:Number;
		
		private var _slider:Sprite;
		
		private var hMinus:Number;
		private var _fadeTime:Number = 0.4;
		
		private var _members:Vector.<EquipIcon> = new Vector.<EquipIcon>;
		////////////////////////////////////////////////////////
		public function get members():Vector.<EquipIcon>
		{
			return _members;
		}
		
		public function ScrollBar(Width:int=400,Height:int=116)
		{
			scrollWidth = Width;
			scrollHeight = Height;
			
			_slider = new Sprite;
			addChild(_slider);
			
			_slider.addEventListener(MouseEvent.MOUSE_DOWN,markPos);//图标栏横移的侦听
		}

		public function addIcon(Icon:EquipIcon):void
		{
			_slider.addChild(Icon);
			_members.push(Icon);
			
			var num:int = _slider.numChildren-1;
			Icon.x = int(num*Icon.width +Icon.width/2) ;
			Icon.y = int(Icon.height/2);
			
			_itemWid = Icon.width;
			
			Icon.addEventListener(MouseEvent.MOUSE_DOWN,onMemberMouseDown);//选中图标的侦听
		}
		
		public function removeIcon(id:uint):void
		{
			var icon:EquipIcon = _members[id];
			icon.removeEventListener(MouseEvent.MOUSE_DOWN,onMemberMouseDown);
			
			_slider.removeChild(icon);
			_members.splice(id,1);
			
			for(var i:int=id;i<_members.length;i++)
			{
				icon = _members[i];
				Tweener.addTween(icon, {x:int(i*icon.width +icon.width/2), time:_fadeTime, transition:"easeoutquad" });
			}
		}
		
		//////////////////////////////////////////////////////////////当鼠标点下原地抬起，则选中当前图标，如果按下鼠标后鼠标有移动，则取消
		private function onMemberMouseDown(e:MouseEvent):void
		{
			var equipICon:EquipIcon = e.currentTarget as EquipIcon;
			
			equipICon.addEventListener(MouseEvent.MOUSE_UP,selectMember);
			equipICon.addEventListener(MouseEvent.MOUSE_MOVE,cancelMemberMove);
		}
		
		private function selectMember(e:MouseEvent):void
		{
			Global.stage.removeEventListener(MouseEvent.MOUSE_MOVE,cancelMemberMove);
			
			var id:int = currentID;
			var equipICon:EquipIcon = e.currentTarget as EquipIcon;
			currentID = _members.indexOf(equipICon);
			
			if(currentID!=id)dispatchEvent(new GameEvent(GameEvent.ITEM_CALL_SELECT));//如果当前id与点击的图标的id不同，则发送选中图标事件
			else dispatchEvent(new GameEvent(GameEvent.ITEM_CALL_MENU));//如果当前id与点击的图标的id相同，则发送打开功能菜单的事件
		}
		
		private function cancelMemberMove(e:MouseEvent):void
		{
			var equipICon:EquipIcon = e.currentTarget as EquipIcon;
			equipICon.removeEventListener(MouseEvent.MOUSE_UP,selectMember);
			equipICon.removeEventListener(MouseEvent.MOUSE_MOVE,cancelMemberMove);
		}
		
		////////////////////////////////////////////////////////图标栏横移的逻辑方法
		private function markPos(e:MouseEvent):void
		{
			hMinus = scrollWidth-_slider.numChildren*(116);
			
			_markMouseX = Global.stage.mouseX;
			_markItemX = _slider.x;
			
			_slider.addEventListener(Event.ENTER_FRAME,dragItem);
			Global.stage.addEventListener(MouseEvent.MOUSE_UP,stopDragItems);
		}
		
		private function dragItem(e:Event):void//拖拽图标栏
		{
			_vX = Global.stage.mouseX-_v0X;
			_slider.x = _markItemX + Global.stage.mouseX-_markMouseX;
			_v0X = Global.stage.mouseX;
			
			if(hMinus>0)//道具栏长度大于道具加起来的长度时
			{
				if(_slider.x < -_itemWid/2)
				{
					Global.stage.removeEventListener(MouseEvent.MOUSE_UP,stopDragItems);
					_slider.removeEventListener(Event.ENTER_FRAME,dragItem);
					Tweener.addTween(_slider, {x:0, time:_fadeTime, transition:"easeoutquad" });
				}else if(_slider.x > (hMinus+_itemWid/2))
				{
					Global.stage.removeEventListener(MouseEvent.MOUSE_UP,stopDragItems);
					_slider.removeEventListener(Event.ENTER_FRAME,dragItem);
					Tweener.addTween(_slider, {x:hMinus, time:_fadeTime, transition:"easeoutquad" });
				}
				
			}else//道具栏长度小于道具加起来的长度时
			{
				if(_slider.x > _itemWid/2 )
				{
					Global.stage.removeEventListener(MouseEvent.MOUSE_UP,stopDragItems);
					_slider.removeEventListener(Event.ENTER_FRAME,dragItem);
					Tweener.addTween(_slider, {x:0, time:_fadeTime, transition:"easeoutquad" });
				}else if(_slider.x < (hMinus-_itemWid/2))
				{
					Global.stage.removeEventListener(MouseEvent.MOUSE_UP,stopDragItems);
					_slider.removeEventListener(Event.ENTER_FRAME,dragItem);
					Tweener.addTween(_slider, {x:hMinus, time:_fadeTime, transition:"easeoutquad" });
				}
			}
		}
		
		private function stopDragItems(e:MouseEvent):void//释放拖拽
		{
			Global.stage.removeEventListener(MouseEvent.MOUSE_UP,stopDragItems);
			_slider.removeEventListener(Event.ENTER_FRAME,dragItem);
			
			if(hMinus>0)//道具栏长度大于道具加起来的长度时
			{
				if(_slider.x<0 )Tweener.addTween(_slider, {x:0, time:_fadeTime, transition:"easeoutquad" });
				else if(_slider.x > hMinus)Tweener.addTween(_slider, {x:hMinus, time:_fadeTime, transition:"easeoutquad" })
				else _slider.addEventListener(Event.ENTER_FRAME,inertiaMotionX);
			}else//道具栏长度小于道具加起来的长度时
			{
				if(_slider.x > 0)Tweener.addTween(_slider, {x:0, time:_fadeTime, transition:"easeoutquad" });
				else if(_slider.x < hMinus)Tweener.addTween(_slider, {x:hMinus, time:_fadeTime, transition:"easeoutquad" });
				else _slider.addEventListener(Event.ENTER_FRAME,inertiaMotionX);
			}
		}
		
		private function inertiaMotionX(e:Event):void//鼠标释放后的惯性
		{
			if(hMinus>0)//道具栏长度大于道具加起来的长度时
			{
				if(_slider.x < -_itemWid/2)
				{
					_slider.removeEventListener(Event.ENTER_FRAME,inertiaMotionX);
					Tweener.addTween(_slider, {x:0, time:_fadeTime, transition:"easeoutquad" });
				}else if(_slider.x > (hMinus + _itemWid/2))
				{
					_slider.removeEventListener(Event.ENTER_FRAME,inertiaMotionX);
					Tweener.addTween(_slider, {x:hMinus, time:_fadeTime, transition:"easeoutquad" });
					
				}else
				{
					if(_vX>30)_vX=30;
					_vX *= 0.9;
					_slider.x += _vX;
					if(Math.abs(_vX)<0.05)_slider.removeEventListener(Event.ENTER_FRAME,inertiaMotionX);
				}
			}else//道具栏长度小于道具加起来的长度时
			{
				if(_slider.x > _itemWid/2 )
				{
					_slider.removeEventListener(Event.ENTER_FRAME,inertiaMotionX);
					Tweener.addTween(_slider, {x:0, time:_fadeTime, transition:"easeoutquad" });
				}else if(_slider.x < (hMinus-_itemWid/2))
				{
					_slider.removeEventListener(Event.ENTER_FRAME,inertiaMotionX);
					Tweener.addTween(_slider, {x:hMinus, time:_fadeTime, transition:"easeoutquad" });
				}else
				{
					if(_vX>30)_vX=30;
					_vX *= 0.9;
					_slider.x += _vX;
					if(Math.abs(_vX)<0.05)_slider.removeEventListener(Event.ENTER_FRAME,inertiaMotionX);
				}
			}
			
		}
		
	}
}